<?php
// 应用公共文件

if (!function_exists('getSiteUrl')) {
    /**
     * 获取当前站点的访问路径根目录
     * @return [type] [description]
     */
    function getSiteUrl()
    {
        $uri = $_SERVER['REQUEST_URI'] ? $_SERVER['REQUEST_URI'] : ($_SERVER['PHP_SELF'] ? $_SERVER['PHP_SELF'] : $_SERVER['SCRIPT_NAME']);
        return 'http://' . $_SERVER['HTTP_HOST'] . substr($uri, 0, strrpos($uri, '/') + 1);
    }
}

if (!function_exists('getDomain')) {
    /**
     * 获取当前站点的访问路径根目录
     * @return [type] [description]
     */
    function getDomain()
    {
        $uri = $_SERVER['REQUEST_URI'] ? $_SERVER['REQUEST_URI'] : ($_SERVER['PHP_SELF'] ? $_SERVER['PHP_SELF'] : $_SERVER['SCRIPT_NAME']);
        $port=$_SERVER['SERVER_PORT'];

        return 'http://' . $_SERVER['HTTP_HOST'] . substr($uri, 0, strrpos($uri, '/') + 1);
    }
}

if (!function_exists('getVirRootDir')) {
    /**
     * 获取虚拟目录路径
     * @return bool|string
     */
    function getVirRootDir()
    {
        $url = $_SERVER['SCRIPT_NAME'];
        $url = substr($url, 0, strripos($url, "/") );
        return $url;
    }
}

if (!function_exists('returnFormat')) {
    /**
     * 格式化输出返回值
     * @param int $code
     * @param string $msg
     * @param array $data
     * @return mixed
     */
    function returnFormat($code=0,$msg="",$data=[]){
        $res['code']=$code;
        $res['data']=$data;
        $res['msg']=$msg;
        return $res;
    }
}

if (!function_exists('getNow')) {
    /**
     * 获取当时时间
     * @param string $fmt 格式化
     * @return false|string
     */
    function getNow($fmt = "Y-m-d H:i:s")
    {
        return date($fmt);
    }
}

if (!function_exists('getUrl')) {
    /**
     * 获取当前站点的访问路径根目录
     * @return [type] [description]
     */
    function getUrl()
    {
        $sys_protocal = isset($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] == '443' ? 'https://' : 'http://';
        $php_self = $_SERVER['PHP_SELF'] ? $_SERVER['PHP_SELF'] : $_SERVER['SCRIPT_NAME'];
        $path_info = isset($_SERVER['PATH_INFO']) ? $_SERVER['PATH_INFO'] : '';
        $relate_url = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : $php_self.(isset($_SERVER['QUERY_STRING']) ? '?'.$_SERVER['QUERY_STRING'] : $path_info);
        return $sys_protocal.(isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : '').$relate_url;
    }
}

if (!function_exists('getallheaders'))
{
    function getallheaders()
    {
        $headers = array ();
        foreach ($_SERVER as $name => $value)
        {
            if (substr($name, 0, 5) == 'HTTP_')
            {
                $headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value;
            }
        }
        return $headers;
    }
}


if (!function_exists('createSn')) {
    function createSn($unique = 0)
    {
        $date = date('YmdHis_');
        return $date . substr(implode(NULL, array_map('ord', str_split(substr(uniqid(),
                7, 13), 1))), 0, 8);
    }
}

if (!function_exists('get_time')) {
    /**
     * 格式化时间
     * @param string $format
     * @param $time
     * @return false|string
     */
    function get_time($format="Y-m-d H:i:s",$time=-1){
        if($time==-1){
            $time=time();
        }
        return date($format,$time);
    }

}

if (!function_exists('writeln')) {
    function writeln($obj){
        print_r($obj);
        echo "\n";
    }
}

if (!function_exists('debug_writeln')) {
    function debug_writeln($obj,$debug=false){
        if($debug){
            print_r($obj);
            echo "\n";
        }
    }
}